import datetime

from Seats.models import Seats_Status
from utils.connect_mysql import connect_outbound


def seats_tatus_save(*args,**kwargs):
    seats_number = kwargs.get('seats_number','')
    status = kwargs.get('status','')
    free_time = kwargs.get('free_time','')
    account_name = kwargs.get('account_name','')
    account_tag = kwargs.get('account_tag','')
    table_name = "zb_seats_status_%s" % str(datetime.datetime.now()).split(' ')[0].replace('-', '_')
    # 连接数据库
    connect = connect_outbound()
    cursor = connect.cursor()
    try:
        sql = "select count(*) from information_schema.tables where table_name ='%s';"%table_name
        cursor.execute(sql)
        count = cursor.fetchone()[0]
        if count == 0:
            sql1 = "create table %s like zb_seats_status" % table_name
            cursor.execute(sql1)
        # 保存数据
        sql2 = "insert into %s (seats_number,status,free_time,account_name,account_tag) values('{}','{}','{}','{}','{}',)"%table_name.format(seats_number,status,free_time,account_name,account_tag)
        cursor.execute(sql2)
        connect.commit()
        cursor.close()
        connect.close()
        return {"code":200,"msg":"保存成功"}
    except Exception as e:
        return {"code":500,"msg":"系统繁忙","error":str(e)}
